﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;

namespace KeaoMDS.InfoManage
{
    public partial class YunShuGongSiBianJi : BaseForm
    {
        private DataGridViewRow m_SelectRow = null;

        public YunShuGongSiBianJi()
        {
            InitializeComponent();
        }

        public YunShuGongSiBianJi(DataGridViewRow SelectRow)
        {
            InitializeComponent();
            m_SelectRow = SelectRow;
        }

        private void YunShuGongSiBianJi_Load(object sender, EventArgs e)
        {
            if (m_SelectRow != null)
            {
                txt_gongsimingcheng.Text = string.Format("{0}", m_SelectRow.Cells["col_gongsimingcheng"].Value);
                chk_gongsizhuangtai.Checked = m_SelectRow.Cells["col_gongsizhuangtai"].Value.Equals("启用") ? true : false;
            }
        }

        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {
            if (m_SelectRow == null)
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("gongsimingcheng", DataValueCommonType.字符串, txt_gongsimingcheng.Text, "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_yunshugongsi", createCondition);
                if (Dt.Rows.Count > 0)
                {
                    KeaoUtils.ShowInfoMessage("已经存在该运输公司名称，请检查重试。");
                    return;
                }
                else
                {
                    TableRow newRow = new TableRow("tbl_yunshugongsi", KeaoUtils.GetGUID(), DBCtl.插入);
                    newRow.DataCols.Add(new TableCol() { ColName="gongsimingcheng",ValueMember=txt_gongsimingcheng.Text, ValueType= DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName="gongsizhuangtai", ValueMember=chk_gongsizhuangtai.Checked?"启用":"禁用", ValueType= DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "站点"), ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "客户编号"), ValueType = DataValueCommonType.字符串 });
                    if (KeaoUtils._DB.SaveTableRows(newRow) > 0)
                    {
                        DialogResult = DialogResult.OK;
                    }
                }
            }
            else
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("gongsimingcheng", DataValueCommonType.字符串, txt_gongsimingcheng.Text, "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串,string.Format("{0}",m_SelectRow.Cells["col_id"].Value), "", SearchConditionType.不等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_yunshugongsi", createCondition);
                if (Dt.Rows.Count > 0)
                {
                    KeaoUtils.ShowInfoMessage("已经存在该运输公司名称，请检查重试。");
                    return;
                }
                else
                {
                    TableRow editRow = new TableRow("tbl_yunshugongsi", string.Format("{0}", m_SelectRow.Cells["col_id"].Value), DBCtl.修改);
                    editRow.DataCols.Add(new TableCol() { ColName = "gongsimingcheng", ValueMember = txt_gongsimingcheng.Text, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "gongsizhuangtai", ValueMember = chk_gongsizhuangtai.Checked ? "启用" : "禁用", ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                    if (KeaoUtils._DB.SaveTableRows(editRow) > 0)
                    {
                        DialogResult = DialogResult.OK;
                    }
                }
            }
        }

        private void menu_btn_guanbi_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
    }
}
